#include "NetcdfDataReader.h"
#include "NcWrapper.h"
#include "NcVarWrapper.h"

namespace OCPP {
	NetcdfDataReader::NetcdfDataReader(GridShape origin, GridShape shape, GridVariablePtr variable)
		:GridDataReader()
	{
		_origin = origin;
		_shape = shape;
		_variable = variable;
	}

	GridDataValuePtr NetcdfDataReader::read(GridData& gridData, const VolumeOptions& options)
	{
		GridDataValuePtr gridDataValue = std::make_shared <GridDataValue>();

		auto& var = static_cast<NcVarWrapper*>(_variable.get())->_var;

		size_t size = 1;
		for (auto& dimShape : _shape)
		{
			size *= dimShape;
		}


		NcType type = var.getType();

		if (type == NC_FLOAT)
		{
			auto& data = gridDataValue->_vecFloat;
			data.resize(size);
			var.getVar(_origin, _shape, &data[0]);
		}
		else if (type == NC_INT)
		{
			auto& data = gridDataValue->_vecFloat;
			data.resize(size);

			IntVector intDataVec;
			intDataVec.resize(size);
			var.getVar(_origin, _shape, &intDataVec[0]);

			float scale = 1.0;
			if (options.customScaler.has_value())
			{
				scale = options.customScaler.value();
			}
			for (auto i=0;i<size;i++)
			{
				const auto& intValue = intDataVec[i];
				data[i] = intValue * scale;
			}
			gridDataValue->doneScaleAndOffset = true;
		}

		return gridDataValue;
	}
}